package org.jeecg.modules.appuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.appuser.constant.TaskNamesConstant;
import org.jeecg.modules.appuser.entity.TbUserCash;
import org.jeecg.modules.appuser.entity.TbUserIntegralConfigDetail;
import org.jeecg.modules.appuser.entity.TbUserLuckdraw;
import org.jeecg.modules.appuser.entity.TbUserLuckdrawDetail;
import org.jeecg.modules.appuser.mapper.TbUserLuckdrawMapper;
import org.jeecg.modules.appuser.service.*;
import org.jeecg.modules.mall.entity.TbMnUser;
import org.jeecg.modules.mall.service.ITbMnUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 抽奖配置表
 * @Author: jeecg-boot
 * @Date: 2020-05-18
 * @Version: V1.0
 */
@Service
public class TbUserLuckdrawServiceImpl extends ServiceImpl<TbUserLuckdrawMapper, TbUserLuckdraw> implements ITbUserLuckdrawService {

    @Autowired
    @Lazy(true)
    private ITbMnUserService tbMnUserService;

    @Autowired
    @Lazy(true)
    private ITbUserLuckdrawDetailService tbUserLuckdrawDetailService;

    @Autowired
    private ITbUserIntegralConfigDetailService tbUserIntegralConfigDetailService;

    @Autowired
    private ITbUserCashService tbUserCashService;


    @Override
    public Result<?> getUsableLuckDrawConfigList(String user_id, Integer luckdraw_type) {
        Map<String, Object> resultMap = new HashMap<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state", 1);
        queryWrapper.le("start_system_time", new Date());
        queryWrapper.ge("end_system_time", new Date());
        TbUserLuckdraw tbUserLuckdraw = this.getOne(queryWrapper);
        if (null != tbUserLuckdraw) {
            resultMap.put("tbUserLuckdraw", tbUserLuckdraw);
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("luckdraw_id", tbUserLuckdraw.getId());
            int luckdraw_Total_num = 0;
            List<TbUserLuckdrawDetail> tbUserLuckdrawDetailList = tbUserLuckdrawDetailService.list(queryWrapper2);
            if (null != tbUserLuckdrawDetailList && tbUserLuckdrawDetailList.size() > 0) {
                luckdraw_Total_num = tbUserLuckdrawDetailList.size();
            }
            resultMap.put("luckdraw_Total_num", luckdraw_Total_num);

            QueryWrapper queryWrapper3 = new QueryWrapper();
            queryWrapper3.eq("luckdraw_id", tbUserLuckdraw.getId());
            queryWrapper3.eq("user_id", user_id);
            int my_luckdraw_Total_num = 0;
            List<TbUserLuckdrawDetail> my_luckdraw_totalList = tbUserLuckdrawDetailService.list(queryWrapper3);
            if (null != my_luckdraw_totalList && my_luckdraw_totalList.size() > 0) {
                my_luckdraw_Total_num = my_luckdraw_totalList.size();
            }
            resultMap.put("my_luckdraw_Total_num", my_luckdraw_Total_num);
            //查看几次广告
            List<TbUserIntegralConfigDetail> tbUserIntegralConfigDetailList = tbUserIntegralConfigDetailService.getTodayTaskByNameList(TaskNamesConstant.Task_LOOK_AVD, user_id);
            int look_avd_num = 0;
            if (null != tbUserIntegralConfigDetailList && tbUserIntegralConfigDetailList.size() > 0) {
                look_avd_num = tbUserIntegralConfigDetailList.size();
            }
            resultMap.put("look_avd_num", look_avd_num);
        }

        return Result.ok(resultMap);


    }

    @Override
    public Result<?> getLuckDrawList() {
        Map<String, Object> resultMap = new HashMap<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        // queryWrapper.eq("state", 1);
        queryWrapper.le("start_system_time", new Date());
        queryWrapper.ge("end_system_time", new Date());
        List<TbUserLuckdraw> list = this.list(queryWrapper);
        if (null == list || list.size() == 0) {
            resultMap.put("state", 0);
        } else {
            resultMap.put("state", 1);
            resultMap.put("list", list);
        }

        return Result.ok(resultMap);
    }

    @Override
    public Result<?> getWinPrizeInfo(TbUserLuckdraw tbUserLuckdraw) {
        if (null == tbUserLuckdraw.getId()) {
            return Result.error("参数错误");
        }
        Map<String, Object> resultMap = new HashMap<>();
        TbUserLuckdraw tbUserLuckdrawList = this.baseMapper.listAndPhone(tbUserLuckdraw);
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("luckdraw_id", tbUserLuckdraw.getId());
        int luckdraw_Total_num = 0;
        List<TbUserLuckdrawDetail> tbUserLuckdrawDetailList = tbUserLuckdrawDetailService.list(queryWrapper2);
        if (null != tbUserLuckdrawDetailList && tbUserLuckdrawDetailList.size() > 0) {
            luckdraw_Total_num = tbUserLuckdrawDetailList.size();
        }
        resultMap.put("luckdraw_Total_num", luckdraw_Total_num);
        resultMap.put("tbUserLuckdrawList", tbUserLuckdrawList);
        return Result.ok(resultMap);
    }

    @Override
    public Result<?> getGameCenterNotice() {

        List<TbUserLuckdraw> tbUserLuckdrawList = this.baseMapper.listLuckData();

        Map<String, Object> resultMap = new HashMap<>();
        //往期抽奖信息
        resultMap.put("tbUserLuckdrawList", tbUserLuckdrawList);

        QueryWrapper  queryWrapper=new QueryWrapper();
        queryWrapper.eq("apply_state",1);
        List<TbUserCash> tbUserCashList = tbUserCashService.list();
        if(null!=tbUserCashList&&tbUserCashList.size()>0){
            for (TbUserCash tbUserCash : tbUserCashList) {

               TbMnUser tbMnUser= tbMnUserService.getById(tbUserCash.getUserId());
                tbUserCash.setPhone(tbMnUser.getPhone());
            }

        }
        resultMap.put("tbUserCashList", tbUserCashList);
        return Result.ok(resultMap);
    }
}
